package com.jiyinit.web.core.business.controller;

import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.web.core.business.service.ResourceServiceImpl;
import com.jiyinit.web.core.business.vo.ResourceVo;
import com.jiyinit.web.core.common.util.UserUtil;
import com.jiyinit.web.core.common.vo.CurrentUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author lishaochuan
 * @version 1.0
 * @date 2017/4/11 14:04
 * @since 1.0
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);

    @Resource(name = "core.resourceServiceImpl")
    private ResourceServiceImpl resourceService;


    /**
     * 跳转首页
     *
     * @param
     * @return
     * @author lishaochuan
     * @create 2017/4/12 9:49
     */
    @RequestMapping("")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        CurrentUserVo currentUser = UserUtil.getCurrentUser();
        if(Check.isNullObj(currentUser)){
            return "login/login";
        }
        Integer userId = currentUser.getUserId();
        if (Check.isNullObj(userId)) {
            request.setAttribute("errorMsg", "对不起，你还没有访问系统权限，请联系管理员设置相关权限。");
            return "/error/error";
        }
        List<ResourceVo> resources = resourceService.findResourceByUser(userId);
        if (Check.isNullCollection(resources)) {
            request.setAttribute("errorMsg", "对不起，你还没有访问系统权限，请联系管理员设置相关权限。");
            return "/error/error";
        }
        // 首次登陆把用户url权限查询出来
        if(Check.isNullCollection(currentUser.getUrlSet())){
            Set<String> urlSet = new HashSet<>();
            this.fillUrlSet(resources, urlSet);
            currentUser.setUrlSet(urlSet);
            UserUtil.setUser2Session(currentUser);
        }
        request.setAttribute("userName", currentUser.getUserName());
        request.setAttribute("resources", resources);
        return "/index/index";
    }


    /**
     * 获取用户所有权限url集合
     * @author lishaochuan
     * @create 2017/4/13 15:50
     * @param
     * @return
     */
    private void fillUrlSet(List<ResourceVo> resources, Set<String> urlSet){
        for (ResourceVo resource : resources) {
            urlSet.add(resource.getResourceUrl());
            if(!Check.isNullCollection(resource.getChildRes())){
                this.fillUrlSet(resource.getChildRes(), urlSet);
            }
        }
        return;
    }


    /**
     * 首页
     * @author lishaochuan
     * @create 2017/4/13 16:02
     * @param
     * @return
     */
    @RequestMapping("homePage")
    public String homePage(HttpServletRequest request, HttpServletResponse response) {
        return "/index/homePage";
    }
}
